package org.xtense.model.actions;

import java.lang.reflect.Method;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import org.dom4j.Node;
import org.xtense.engine.actions.UpdateAction;

public abstract class Interceptor implements MethodInterceptor {
	protected Enhancer enhancer;
	protected boolean transaction;

	public abstract Object intercept(Object o, Method method, Object[] args,
			MethodProxy proxy) throws Throwable;
	
	public void setTransaction(boolean bt) {
		transaction = bt;
	}
	
	protected int getDocIDFromNode(Node userORus) {
		while (ResultCache.getDocumentIdFromNode(userORus) < 0) {
			userORus = userORus.getParent();
		}
		return ResultCache.getDocumentIdFromNode(userORus);
	}

	protected int getElementPreFromNode(int docID, Node userORus) {
		int elementPre = ResultCache.getElementPreFromNode(userORus);
		if (elementPre < 0) {
			elementPre = UpdateAction.getInstance(docID).getCache().getPre(
					userORus)[0];
		}
		return elementPre;
	}

	protected int getParentPreFromNode(int docID, Node userORus) {
		int parentPre = ResultCache.getParentPreFromNode(userORus);
		if (parentPre < 0) {
			parentPre = UpdateAction.getInstance(docID).getCache().getPre(
					userORus)[1];
		}
		return parentPre;
	}

}
